function init_with_angular() {

	angular.element(document).ready(function() {
		window.init();
	});
}

var app = angular.module('ShowupApp', []);

app.controller('AppointmentCtrl', ['$scope', function($scope) {
	moment.locale('he');

	$scope.appointment = {};
	$scope.business_info = {};
	$scope.confimed = false;
	$scope.declined = false;
	$scope.is_backend_ready = false;
	$scope.error = false;

	window.init = function() {
		$scope.$apply($scope.load_api);
	};

	$scope.load_api = function() {
		var ROOT = 'https://showup-app.appspot.com/_ah/api';
		gapi.client.load('showup', 'v1', function() {

			var urlSplit = window.location.href.split("/");
			$scope.id = urlSplit[urlSplit.length - 2];
			$scope.token = urlSplit[urlSplit.length - 1];
			$scope.getAppointmentDetails();

		}, ROOT);
	}	

	$scope.appointmentFromNow = function() {
		return moment(parseInt($scope.appointment.start_time) * 1000).fromNow();
	}


	$scope.appointmentTime = function() {
		return moment(parseInt($scope.appointment.start_time) * 1000).format("dddd, HH:mm");
	}

	$scope.confirmAppointment = function() {
		gapi.client.showup.appointment.confirm({id: $scope.id, token: $scope.token}).execute(function(resp) {
			$scope.confirmed = true;
		});
	}

	$scope.declineAppointment = function() {
		gapi.client.showup.appointment.decline({id: $scope.id, token: $scope.token}).execute(function(resp) {
			$scope.declined = true;
		});
	}

	$scope.getAppointmentDetails = function() {
		gapi.client.showup.appointment.get({id: $scope.id, token: $scope.token}).execute(function(resp) {
			$scope.$apply(function () {    
				console.log(resp);
				$scope.is_backend_ready = true;
				if (resp.hasOwnProperty('code'))
					$scope.error = true;
				else {
					$scope.appointment = resp.appointment;
					$scope.business = resp.business;
				}
			});
		});
	}
}]); 